package com.firemoss.consumermediatorexample.control
{
	import mx.controls.Alert;
	import mx.messaging.Producer;
	import mx.messaging.events.MessageFaultEvent;
	import mx.messaging.messages.AsyncMessage;
	import mx.messaging.messages.IMessage;
	
	[Bindable]
	public class SimpleChatController
	{
		
		// EVENTS
		
		public static const CONNECT_TO_CHAT : String = "chatConnectionRequested"
		public static const DISCONNECT_FROM_CHAT : String = "chatDisconnectionRequested"
		public static const CHAT_MESSAGE_RECEIVED : String = "chatMessageReceived"
		public static const CHAT_CONNECTION_CHANGED : String = "chatConnectionChanged"
		public static const MESSAGE_SENT : String = "messageSent"
		public static const CHAT_MESSAGE_FAULT : String = "chatMessageFault"
		
		
		// PUBLIC PROPERTIES
		
		public var chatLog : String = "";
		public var connected : Boolean = false;

		// EVENT LISTENERS
		
		[Mediate( event="chatConnectionChanged", properties="connected" )]
		public function chatConnectionChanged( connected : Boolean ) : void
		{
			this.connected = connected
		}
		
		[Mediate( event="chatMessageReceived", properties="message" )]
		public function consumerResultHandler( message : String ) : void
		{
			chatLog = new Date().toTimeString() + " - " + message + "\n\n" + chatLog
		}
		
		[Mediate( event="chatMessageFault", properties="cause" )]
		public function messageFaultHandler( cause : MessageFaultEvent ) : void
		{
			Alert.show( "Oops!  There was a problem receiving messages.");
		}
		
	}
}